//
//  expressViewController.m
//  LazyCat
//
//  Created by rocky on 16/1/5.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "expressViewController.h"
 
#import "clientSearchViewController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
#import "ZBarScanVC.h"

@interface expressViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate,SendNumDelegate>

{
    UIPageViewController *_pageViewCtrl;
    NSMutableArray *_dataArray;
    NSInteger _currentPage;
    UIView *_bgView;
    UIView *_lineView;
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    BOOL _finishLoad;//web页是否加载完毕
    UIView *_noNetView;
    
}
 
@property (nonatomic,strong) UIWebView *webV;
@property (nonatomic,strong) UIButton *btn;
@end

@implementation expressViewController




- (void)backAction:(UIButton *)btn {
    if (![self.webV canGoBack]) {//判断是否可以HTML返回
        if (!self.navigationController.viewControllers) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else {
        [self.webV goBack];
        [self.webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUr]]];
        if (!self.btn) {
            self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.btn setTitle:@"关闭" forState:UIControlStateNormal];
            self.btn.frame = CGRectMake(20, 27, 50, 30);
            [self.btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            self.btn.titleLabel.font = [UIFont systemFontOfSize:16];
            [self.navBar addSubview:self.btn];
            [self.btn addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
}
-(void)popViewController {
    NSArray *arr = self.navigationController.viewControllers;
    [self.navigationController popToViewController:arr[1] animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.navBar addSubview:_progressView];
    if (kNetworkNotReachability) {
        _webV.hidden = YES;
        _noNetView.hidden = NO;
    }else{
        _webV.hidden = NO;
        _noNetView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"快递服务" doBack:YES];
    self.view.backgroundColor = BackColor;
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.alpha = 0.2;
    lineV.backgroundColor = [UIColor lightGrayColor];
    [self.navBar addSubview:lineV];
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(ScreenWidth-50, 27, 50, 30);
    [searchBtn setImage:[UIImage imageNamed:@"searchLog"] forState:UIControlStateNormal];
    searchBtn.imageEdgeInsets = UIEdgeInsetsMake(4, 14, 4, 14);
    [searchBtn setContentMode:UIViewContentModeScaleAspectFit];
    [searchBtn addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];//搜索按钮
    [self.navBar addSubview:searchBtn];
    self.webV = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    self.webV.delegate = self;
#pragma mark -- 进度条1
    _progressProxy = [[NJKWebViewProgress alloc]init];
    self.webV.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    CGRect navBounds = self.navBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, 0, 2);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth - ScreenWidth/2+80, 2);
    } completion:^(BOOL finished) {
        //        if (_finishLoad) {
        //            _progressView.hidden = YES;
        //        }
    }];
    [self.webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUr]]];
    [self.webV.scrollView setScrollEnabled:YES];
    self.webV.backgroundColor = BackColor;
    [self.view addSubview:self.webV];
    self.webV.scrollView.showsVerticalScrollIndicator = NO;
    
    [self createNoNetView];
}
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth, 2);
    } completion:^(BOOL finished) {
        _finishLoad = YES;
        _progressView.hidden = YES;
    }];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    _progressView.hidden = YES;
}

-(void)createNoNetView
{
    _noNetView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
    _noNetView.hidden = YES;
    _noNetView.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:243 / 255.0 blue:247 / 255.0 alpha:1];
    [self.view addSubview:_noNetView];
    UIImageView *netImg = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-100)/2, 60, 100, 90)];
    netImg.image = [UIImage imageNamed:@"无网络_r1_c1"];
    [_noNetView addSubview:netImg];
    UILabel *netFail = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, ScreenWidth, 20)];
    netFail.text = @"网络请求失败";
    netFail.textAlignment = NSTextAlignmentCenter;
    netFail.font = [UIFont systemFontOfSize:16];
    [_noNetView addSubview:netFail];
    UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-120)/2, 200, 120, 40)];
    [refresh setImage:[UIImage imageNamed:@"无网络_r3_c2 下午4.23.04"] forState:UIControlStateNormal];
    refresh.adjustsImageWhenHighlighted = NO;
    [_noNetView addSubview:refresh];
    [refresh addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventTouchUpInside];
}

-(void)refreshAction {//刷新方法
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        _noNetView.hidden = NO;
        _webV.hidden = YES;
    }else {
        _noNetView.hidden = YES;
        _webV.hidden = NO;
        
        [_webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUr]]];
    }
}
-(void)searchBtn {//点击跳转到搜索页面
    NSLog(@"搜索");
    clientSearchViewController *cSearchVC = [[clientSearchViewController alloc]init];
    cSearchVC.isFrom = @"kd";
    [self.navigationController pushViewController:cSearchVC animated:YES];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSLog(@"%@",requestString);
    NSArray *components = [requestString componentsSeparatedByString:@"@"];//提交请求时候分割参数的分隔符
    if (components.count == 2) {
        NSArray *resultArr = [components[1] componentsSeparatedByString:@":"];
        //        NSLog(@"=====================%ld",(unsigned long)resultArr.count);
        //        NSLog(@"%@ ==== %@ === %@",resultArr[0],resultArr[1],resultArr[2]);
        if ([resultArr count] > 1 && [(NSString *)[resultArr objectAtIndex:0] isEqualToString:@"lanmao_ios"]) {
            //过滤请求是否是我们需要的.不需要的请求不进入条件
            NSString *actionStr = [resultArr objectAtIndex:1];
            NSLog(@"%@",actionStr);
           if ([actionStr isEqualToString:@"scanExpress"]){
                ZBarScanVC *zbar = [[ZBarScanVC alloc] init];
                zbar.delegate = self;
                [self.navigationController pushViewController:zbar animated:YES];
                return NO;
            }else {
                NSLog(@"----------------");
            }
        }
    }
    return YES;
}
- (void)senNum:(NSString *)num {
    [self.webV stringByEvaluatingJavaScriptFromString:num];
}





@end
